<?php
declare(strict_types=1);

namespace app\common\business;

use app\common\model\Flag as FlagModel;
use think\facade\Log;

class Flag extends CommonBase
{
    private $_flagObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode = $websiteCode;
        $this->_flagObj     = new FlagModel([], $websiteCode);
    }

    public function getAllFlag(array $where = []): array
    {
        $rows = [];
        try {
            $rows = $this->_flagObj->getAllFlag($where);
        } catch (\Exception $e) {
            logErr('读取所有文档标签失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows;
    }

    public function getFlagList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $res   = [];
        $count = 0;
        try {
            $res   = $this->_flagObj->getFlagList($page, $limit, $where);
            $count = $this->_flagObj->getFlagCount($where);
        } catch (\Exception $e) {
            logErr('读取文档标签列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return ['rows' => $res ? $res->toArray() : [], 'count' => $count];
    }

    public function addFlag(array $data): array
    {
        try {
            return [self::_RESULT => $this->_flagObj->addFlag($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加文档标签失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findFlagById(int $id): array
    {
        $attribute = null;
        try {
            $attribute = $this->_flagObj->findFlagById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取文档标签详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($attribute)) {
            return [];
        }

        return $attribute->toArray();
    }

    public function updateFlag(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_flagObj->updateFlag($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新文档标签失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteFlag(int $id): array
    {
        try {
            return [self::_RESULT => $this->_flagObj->deleteFlag($id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('删除文档标签失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
